﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AISFMIOperator
{
     class Patient : Owner
    {
        public Patient()
        {
        }

        public int PatientID
        { get; set; }

        public string LastName
        { get; set; }

        public string FirstName
        { get; set; }

        public string MiddleName
        { get; set; }

        public string ContactPhone
        { get; set; }

        public string Email
        { get; set; }

        public int Age
        { get; set; }

        public DateTime DateOfBirthday
        { get; set; }

        public char Sex
        { get; set; }

        public float Height
        { get; set; }

        public float Weight
        { get; set; }

        public Address Address
        { get; set; }

        public string FullAddress
        { get { return Address.FullAddress; } }

        public List<Insurance> Insurances
        { get; set; }
        
        public string GetFullName()
        {
            return this.LastName + " " + this.FirstName + (this.MiddleName == null ? "" : this.MiddleName);
        }
                
        public int GetOwnerID()
        {
            return this.PatientID;
        }

        public byte GetOwnerType()
        {
            return 1;
        }

        public void Save()
        {
            SqlCommand command = new SqlCommand("CreatePatient");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@lname", SqlDbType.VarChar, 50).Value = LastName;
            command.Parameters.Add("@fname", SqlDbType.VarChar, 50).Value = FirstName;
            command.Parameters.Add("@mname", SqlDbType.VarChar, 50).Value = MiddleName;
            command.Parameters.Add("@dob", SqlDbType.DateTime).Value = DateOfBirthday;
            command.Parameters.Add("@sex", SqlDbType.Char, 1).Value = Sex;
            command.Parameters.Add("@weight", SqlDbType.Float).Value = Weight;
            command.Parameters.Add("@height", SqlDbType.Float).Value = Height;
            command.Parameters.Add("@address_id", SqlDbType.Int).Value = Address.AddressID;
            command.Parameters.Add("@email", SqlDbType.VarChar, 100).Value = Email;
            command.Parameters.Add("@contact_phone", SqlDbType.VarChar, 20).Value = ContactPhone;
	        command.Parameters.Add("@patient_id", SqlDbType.Int).Direction = ParameterDirection.Output;
            
            SQLDBLayer.ExecSPNonQuery(command);
            this.PatientID = (int)command.Parameters["@patient_id"].Value;

            command = new SqlCommand("AddInsuranceToPatient");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@patient_id", SqlDbType.Int);
            command.Parameters.Add("@insurance_id", SqlDbType.Int);

            foreach (Insurance insurance in Insurances)
            {
                if (insurance.Owner.GetOwnerType() == 1)
                {
                    insurance.Owner = this; 
                }
                insurance.Save();

                command.Parameters["@patient_id"].Value = this.PatientID;
                command.Parameters["@insurance_id"].Value = insurance.InsuranceID;
                SQLDBLayer.ExecSPNonQuery(command);
            }

        }

        //public List<Patient> GetAllPatients()
        //{
        //    List<Patient> patients = new List<Patient>();


        //}

    }
}
